package com.javasee.rain.stock.service;

import com.javasee.rain.stock.mapper.ZhangtingMapper;
import com.javasee.rain.stock.model.Zhangting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 涨停数据服务类
 * 
 * @author : xinyanfei
 * @date : 2025/10/10
 * Copyright (c) 2025, 京东商城  All Rights Reserved.
 */
@RestController
@RequestMapping("/api/zhangting")
@CrossOrigin(origins = "*") // 允许跨域访问
public class ZhangtingService {

    @Autowired
    private ZhangtingMapper zhangtingMapper;

    /**
     * 根据日期获取涨停数据
     */
    @GetMapping("/{dateTime}")
    public ResponseEntity<List<Zhangting>> getZhangtingByDate(@PathVariable String dateTime) {
        try {
            System.out.println("查询涨停数据，日期: " + dateTime);

            if(!StringUtils.isEmpty(dateTime)){
                dateTime = dateTime.replace("-","");
            }
            System.out.println("查询涨停数据，日期replace: " + dateTime);

            List<Zhangting> zhangtingList = zhangtingMapper.selectByDate(dateTime);
            return ResponseEntity.ok(zhangtingList);
        } catch (Exception e) {
            System.err.println("查询涨停数据失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 根据股票代码获取涨停数据
     */
    @GetMapping("/code/{code}")
    public ResponseEntity<List<Zhangting>> getZhangtingByCode(@PathVariable String code) {
        try {
            System.out.println("查询股票涨停数据，代码: " + code);
            List<Zhangting> zhangtingList = zhangtingMapper.selectByCode(code);
            return ResponseEntity.ok(zhangtingList);
        } catch (Exception e) {
            System.err.println("查询股票涨停数据失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 获取所有涨停数据
     */
    @GetMapping("/get")
    public ResponseEntity<Map<String, Object>> getAllZhangting() {
        try {
           List<Zhangting> zhangtingList = zhangtingMapper.selectList();

            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", zhangtingList);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("获取涨停数据失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }
}